package Offer.offer2016;

public class Test03
{
	public static void main(String[] args) 
	{
		int[][] matrix = {{0,3,6},{1,4,7},{2,5,8}};
//		System.out.println(findNumber(matrix,3));
		System.out.println(findNumber(3,matrix));
	}
    public static boolean findNumber(int[][] matrix,int number)
    {
    	if(matrix==null||matrix.length==0)
    	{
    		return false;
    	}
    	
    	int rows = matrix.length;
    	int columns = matrix[0].length;
    	int i = 0;
    	int j = columns-1 ;
    	while(i<=rows-1&&j>=0)
    	{
    		if(matrix[i][j]==number)
    		{
    			return true;
    		}
    		if(matrix[i][j]<number)
    		{
    			++i;
    		}
    		if(matrix[i][j]>number)
    		{
    			--j;
    		}
    	}
    	return false;
    }
    /**
     * ��Ŀ������ά���飬����������������
     * ĳ��ֵ�Ƿ���������
     */
    public static boolean findNumber(int number,int[][] matrix){
    	if(matrix == null){
    		return false;
    	}
    	int rows = matrix.length;
    	int cols = matrix[0].length;
    	for(int i = 0 ,j = cols - 1; i < rows && j >= 0;){
    		if(matrix[i][j] == number){
    			return true;
    		}else if(matrix[i][j] > number){
    			j -- ;
    		}else if(matrix[i][j] < number){
    			i++;
    		}
    	}
    	return false;
    }
}
